package com.jacky.me.plugin.ui.hook.step2;

import android.os.Bundle;
import android.os.Environment;
import android.view.View;

import com.jacky.me.plugin.R;
import com.jacky.me.plugin.framework.BaseActivity;

import java.io.File;
import java.lang.reflect.Method;

import dalvik.system.DexClassLoader;

public class LoadJarActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load_jar);
    }

    public void loadJar(View view) {
        //dex解压释放后的目录
        final File dexOutPutDir = getDir("dex", 0);
        //dex所在目录
        final String dexPath = Environment.getExternalStorageDirectory().toString() + File.separator + "new_log.jar";

        //第一个参数：是dex压缩文件的路径
        //第二个参数：是dex解压缩后存放的目录
        //第三个参数：是C/C++依赖的本地库文件目录,可以为null
        //第四个参数：是上一级的类加载器
        DexClassLoader classLoader = new DexClassLoader(dexPath, dexOutPutDir.getAbsolutePath(), null, getClassLoader());

        try {
            final Class<?> loadClazz = classLoader.loadClass("com.jacky.me.mylibrary.LogUtils");
            final Object o = loadClazz.newInstance();
            final Method printLogMethod = loadClazz.getDeclaredMethod("printLog");
            printLogMethod.setAccessible(true);
            printLogMethod.invoke(o);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

        /*
     public class LogUtils {
         public static final String TAG = "LogUitl";

        private void printLog() {
            Log.e(TAG, "这是来自另外一个dex中的log");
        }
     }
    */

}
